// ConvView.cpp: implementation of the CAppView class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Rock.h"
#include "AppView.h"
#include "MainToolbar.h"
#include "HightTimer.h"

CAppView::CAppView()
{
	m_Created   = false;
}

CAppView::~CAppView()
{

}

void CAppView::Layout(bool Redraw /* = true */){
	CWSPopView::Layout(Redraw);
	if(m_Created)
	::MoveWindow(m_IOView.GetHwnd(),m_rcClient.left,m_rcClient.top,RectWidth(m_rcClient),RectHeight(m_rcClient),TRUE);
}

LRESULT CAppView::Reaction(UINT message, WPARAM wParam, LPARAM lParam){
   if(message == WM_LBUTTONDOWN)
	   return OnLButtonDown(wParam,lParam);
   else if(message == WM_CREATE)
	   return OnCreate(wParam,lParam);
   else return CWSPopView::Reaction(message,wParam,lParam);
}

LRESULT CAppView::OnCreate(WPARAM wParam, LPARAM lParam){
	DWORD style = GetWindowLong(m_hWnd,GWL_STYLE);
	style |=WS_CLIPCHILDREN;
	SetWindowLong(m_hWnd,GWL_STYLE,style);
	
	RECT rc;
	::SetRect(&rc,0,0,0,0);
	if(!m_IOView.Create(GetHinstance(),NULL,WS_CHILD|WS_VISIBLE|WS_CLIPCHILDREN,rc,GetHwnd())){
		return -1;
	}
	
	m_Created = true;
	return 0;
}
 
LRESULT CAppView::OnLButtonDown(WPARAM wParam, LPARAM lParam) 
{
	POINT point;
	point.x = GET_X_LPARAM(lParam); 
	point.y = GET_Y_LPARAM(lParam);

    CSpace2 * Space = NcHit(point);	
	if(Space){
		int64 ID = Space->m_ID;
		LRESULT ret;
		DockboxItem* DockBnt = GetDockBnt();
		switch(ID){
		case ID_CLOSE:
			{
				Destroy();
				m_Created = false;
				SendParentRectionMessage(WM_DESTROY);
			}
			break;
		case ID_DOCKBNT:
			{
				if(DockBnt->m_IsDock){
					DockBnt->m_IsDock = false;
				}else{
					DockBnt->m_IsDock = true;
				}
				SendParentRectionMessage(CM_DOCKVIEW);
			}
			break;
		case ID_LEFTSIDE:
			ret = SendParentRectionMessage(WM_NCLBUTTONDOWN);
			if(ret == DOCK_RIGHT){
			    m_bDragBorder = true;
				::SendMessage(m_hWnd,WM_NCLBUTTONDOWN,HTLEFT,MAKELPARAM(point.x,point.y));
				RECT rc;
				::GetWindowRect(m_hWnd,&rc);
				m_PopSize = RectWidth(rc);
				m_bDragBorder = false;
			}
            break;		
		case ID_RIGHTSIDE:
			ret = SendParentRectionMessage(WM_NCLBUTTONDOWN);
			if(ret == DOCK_LEFT){
				m_bDragBorder = true;
				::SendMessage(m_hWnd,WM_NCLBUTTONDOWN,HTRIGHT,MAKELPARAM(point.x,point.y));
				RECT rc;
				::GetWindowRect(m_hWnd,&rc);
				m_PopSize = RectWidth(rc);	
				m_bDragBorder = false;
            }
			break;
		case ID_BOTTOMSIDE:
			ret = SendParentRectionMessage(WM_NCLBUTTONDOWN);
			if(ret == DOCK_TOP){
				m_bDragBorder = true;
				::SendMessage(m_hWnd,WM_NCLBUTTONDOWN,HTBOTTOM,MAKELPARAM(point.x,point.y));
				RECT rc;
				::GetWindowRect(m_hWnd,&rc);
				m_PopSize = RectWidth(rc);	
				m_bDragBorder = false;
            }
			break;
		case ID_TITLETEXT: //TextLabel
			ret = SendParentRectionMessage(WM_NCLBUTTONDOWN);
			if(ret == DOCK_BOTTOM){
				if(DockBnt->m_IsDock)return 0;
				m_bDragBorder = true;
				::SendMessage(m_hWnd,WM_NCLBUTTONDOWN,HTTOP,MAKELPARAM(point.x,point.y));
				RECT rc;
				::GetWindowRect(m_hWnd,&rc);
				m_PopSize = RectHeight(rc);	
				m_bDragBorder = false;
            }
			break;
		}

	}
    return ::DefWindowProc(m_hWnd,WM_LBUTTONDOWN,wParam,lParam);
}

/////////////////////////////////////////////////////

AddConvBnt::AddConvBnt(int64 ID,COLORREF crBorder,
						 COLORREF crNormal,
						 COLORREF crFocused,
						 COLORREF crSelected)
						 :CSpace2(ID),
						 m_crBorder(crBorder),
						 m_crNormal(crNormal),
						 m_crFocused(crFocused),
						 m_crSelected(crSelected),
						 m_IsDock(false)
{
};

void AddConvBnt::Draw(HDC hDC){
	COLORREF crDraw = m_crNormal;
	if(m_State&SPACE_FOCUSED){
		crDraw = m_crFocused;
		m_State &=~SPACE_FOCUSED; 
	}
    else if(m_State & SPACE_SELECTED){
		crDraw = m_crSelected;
		m_State &= ~SPACE_SELECTED;
	}
	int   w = m_Area.left+RectWidth(m_Area)/2;
	int32 h = m_Area.top+RectHeight(m_Area)/2;

	// draw +
	RECT rc;
    ::SetRect(&rc,m_Area.left+3,h-1,m_Area.right-3,h+1);
	FillRect(hDC,rc,crDraw);
    ::SetRect(&rc,w-1,m_Area.top+3,w+1,m_Area.bottom-3);
	FillRect(hDC,rc,crDraw);
	DrawEdge(hDC,m_Area,m_crBorder);
}

